package ac.svn.accesscontrol.store;

import java.util.HashSet;
import java.util.Set;

public class SVNUser implements Comparable<SVNUser>,ISVNItem
{
    private String userAlias;
    private String userName;
    private Set<String> groups;
    private String comments;

    public String getUserAlias() {
	return userAlias;
    }
    public void setUserAlias(String userAlias) {
	this.userAlias = userAlias;
    }
    public String getUserName() {
	return userName;
    }
    public void setUserName(String userName) {
	this.userName = userName;
    }
    public Set<String> getGroups() {
	return groups;
    }
    public void setGroups(Set<String> groups) {
	this.groups = groups;
    }

    public void addGroup(String groupId)
    {
	this.groups.add(groupId);
    }

    public boolean isMemberOfGroup(String groupId)
    {
	if(this.groups.contains(groupId))
	{
	    return true;
	}
	else
	{
	    return false;
	}
    }
    public SVNUser(String userAlias, String userName) {
	super();
	this.userAlias = userAlias;
	this.userName = userName;
	this.groups = new HashSet<String>();
    }

    public SVNUser() {
	super();
	this.groups = new HashSet<String>();
    }
    @Override
    public String toString() {
	return String.format("%s [%s]",getUserName(),getUserAlias());
    }
    @Override
    public int compareTo(SVNUser o) {
	return userAlias.toLowerCase().compareTo(o.getUserAlias().toLowerCase());
    }
    @Override
    public String getName() {
	return getUserAlias();
    }
    @Override
    public String getTooltipText() {
	return "<HTML>SVNUser<br>UserAlias=" + userAlias + "<br>UserName=" + userName
	+ "<br>Groups=" + groups + "</HTML>";
    }
    @Override
    public String getId() {
	return userAlias;
    }
    /**
     * @return the comments
     */
    public String getComments() {
	return comments;
    }
    /**
     * @param comments the comments to set
     */
    public void setComments(String comments) {
	this.comments = comments;
    }

    public SVNUser clone()
    {
	SVNUser user = new SVNUser(userAlias,userName);
	Set<String> grps = new HashSet<String>();
	for(String groupId: groups)
	{
	    grps.add(groupId);
	}
	user.setGroups(grps);
	user.setComments(comments);
	
	return user;
    }
    @Override
    public String getMetaData() {
	return String.format("%s|%s|%s", userName,userAlias,groups);
    }

}
